package com.luoye.unionlotto;

import com.glc.http.client.internet.HttpConnectionHelper;
import com.glc.http.client.internet.ParamCollect;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UnionlottoFromNetGetter {
    private String url="http://kaijiang.zhcw.com/zhcw/inc/ssq/ssq_wqhg.jsp?pageNum=1";
    private int totalPage=-1;
    private List<Integer> errorPages;
    protected List<Unionlotto> unionlottoList;

    public UnionlottoFromNetGetter(String url) {
        this.url = url;
        errorPages=new ArrayList<Integer>();
        this.unionlottoList=new ArrayList<Unionlotto>();
    }

    public List<Unionlotto> get(Unionlotto latest){
        String resStr=download(1);
        if(!resStr.equals("")) {
            totalPage = getTotalPage(resStr);
            addUnionlotto(resStr);
        }
        for(int i=2;i<=totalPage;i++){
            if(latest!=null){//判断是否已经获取
                for(Unionlotto unionlotto:this.unionlottoList){
                    if(unionlotto.getDate().equals(latest.getDate())){
                        return this.unionlottoList;
                    }
                }
            }
            resStr = download(i);
            if(!resStr.equals("")) {
                addUnionlotto(resStr);
            }
        }
        System.out.println(this.unionlottoList);
        System.out.println("errorPages->"+errorPages);
        return this.unionlottoList;
    }

    public String download(int pageNum){
        HttpConnectionHelper httpConnectionHelper=new HttpConnectionHelper();
        ParamCollect paramCollect=new ParamCollect();
        paramCollect.addOrSetParam("pageNum",pageNum+"");
        String resStr ="";
        try {
            resStr = httpConnectionHelper.getResponseStr(url, paramCollect.getParamList());
            System.out.println(resStr);
            //getTotalPage(resStr);
            //addUnionlotto(resStr);
        }catch (Exception e){
            e.printStackTrace();
            errorPages.add(pageNum);
        }
        return resStr;
    }

    private int getTotalPage(String rspStr){
        int pages=1;
        //<p class="pg"> 共<strong>128</strong> 页 /<strong>2552 </strong>条记录 <strong><a href="/zhcw/inc/ssq/ssq_wqhg.jsp">首页</a></strong> <strong><a href="/zhcw/inc/ssq/ssq_wqhg.jsp?pageNum=1">上一页</a></strong> <strong><a href="/zhcw/inc/ssq/ssq_wqhg.jsp?pageNum=2">下一页</a></strong> <strong><a href="/zhcw/inc/ssq/ssq_wqhg.jsp?pageNum=128">末页</a></strong> 当前第<strong> 1 </strong>页</p>
        int idxStart=rspStr.indexOf("<p class=\"pg\"> 共<strong>")+24;
        System.out.println(idxStart);
        int idxEnd=rspStr.indexOf("</strong>",idxStart);
        System.out.println(idxEnd);
        String pgs=rspStr.substring(idxStart,idxEnd);
        System.out.println(pgs);
        pages=Integer.parseInt(pgs);
        return pages;
    }

    private void addUnionlotto(String rspStr){
        int idxStart=rspStr.indexOf("<table")+84;
        System.out.println(idxStart);
        int idxEnd=rspStr.indexOf("</table>",idxStart);
        System.out.println(idxEnd);
        String data=rspStr.substring(idxStart,idxEnd);
        System.out.println(data);
        int idx=0;
        while(idx!=-1){
            int idxe=data.indexOf("</tr>",idx);
            String line=data.substring(idx,idxe+5);
            System.out.println(line);
            if(!(line.startsWith("<tr style=\" background:url")||line.startsWith("<tr>          <th width=")||line.startsWith("<tr>          <td colspan="))){
                Unionlotto unionlotto=new Unionlotto();
                int dateB=line.indexOf("<td align=\"center\">")+19;
                int dateE=line.indexOf("</td>",dateB);
                String date=line.substring(dateB,dateE);
                unionlotto.setDate(date);
                System.out.println(date);
                int b=line.indexOf("<em ");
                int e=line.indexOf("</em></td>",b)+5;
                String numbers=line.substring(b,e);
                System.out.println(numbers);
                unionlotto.setRedBalls(new ArrayList<String>());
                int rbidx=0;
                while (rbidx!=-1){
                    int rbidxe=numbers.indexOf("</em>",rbidx);
                    String redBall=numbers.substring(rbidx+15,rbidxe);
                    System.out.print(redBall+"  ");
                    unionlotto.getRedBalls().add(redBall);
                    rbidx=numbers.indexOf("<em class=\"rr\">",rbidxe);
                }
                int bbidx=numbers.indexOf("<em>");
                int bbidxe=numbers.indexOf("</em>",bbidx);
                String blueBall=numbers.substring(bbidx+4,bbidxe);
                unionlotto.setBlueBall(blueBall);
                System.out.println(blueBall+"  ");
                this.unionlottoList.add(unionlotto);
            }
            idx=data.indexOf("<tr>",idxe);
        }
    }

}
